package com.wef.system.service.impl;

import java.util.List;
import java.util.Set;

import com.gfa4j.exception.ValidateRuntimeException;
import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.wef.system.entity.RoleMenuButton;
import com.wef.system.service.RoleMenuButtonService;

public class RoleMenuButtonServiceImpl extends BaseServiceImpl implements RoleMenuButtonService {

	public RoleMenuButton addRoleMenuButton(RoleMenuButton roleMenuButton){
		
		return (RoleMenuButton) super.saveObject(roleMenuButton);
	}

	public void deleteRoleMenuButton(RoleMenuButton roleMenuButton) {		
		super.deleteObject(roleMenuButton);
		
	}

	public RoleMenuButton saveOrUpdateRoleMenuButton(RoleMenuButton roleMenuButton){	
		if (roleMenuButton.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addRoleMenuButton(roleMenuButton);
		}
		if (roleMenuButton.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateRoleMenuButton(roleMenuButton);
		}
		if (roleMenuButton.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.deleteRoleMenuButton(roleMenuButton);
			return null ;
		}
		return roleMenuButton;
	}


	public Set<RoleMenuButton> saveOrUpdateRoleMenuButtons(Set<RoleMenuButton> roleMenuButtons){
		StringBuilder errorString = new StringBuilder();
		for (RoleMenuButton roleMenuButton : roleMenuButtons) {
			try {
				saveOrUpdateRoleMenuButton(roleMenuButton);
			} catch (ValidateRuntimeException ex) {
				errorString.append(ex.getMessage() + "\n");
			}
		}
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
		return roleMenuButtons;
	}

	public RoleMenuButton updateRoleMenuButton(RoleMenuButton roleMenuButton) {
		return (RoleMenuButton) super.updateObject(roleMenuButton);
	}	
	

}
